﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using MyLib;
using ProcessControl;
using System.Collections;
using Portal.Interface;
using Portal.Entity;
using Portal.Logic;
using ProcessControl.Interface;

namespace CapexProcess
{
    public partial class NewCapexProcess : System.Web.UI.Page
    {
        IOrg _org = ServiceContainer.Instance().GetService<IOrg>();
        ICapex capex = ServiceContainer.Instance().GetService<ICapex>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //初始化项目负责人
                BindddlProjectLeader();
                //初始化项目支出原因
                BindddlProjectReason();
            }
            UserEntity user = _org.GetUserEntity(Portal.Logic.SSOLogic.GetLoginName());
            string strUserDept = user.EXT02;
            string strCostCenter = InitCostCenter(user.USERID, user.EXT02);
            fld_OPERATION.Text = JudgeIsOpreation(strCostCenter);          
            ((ButtonList)this.ButtonList1).AfterSubmit += new System.ComponentModel.CancelEventHandler(NewCapexProcess_AfterSubmit);
        }

        void NewCapexProcess_AfterSubmit(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //if (hfMainGuid.Value != "")
            //{
            //    string sqlDele = "  DELETE FROM PROC_CAPEX_PROJECT WHERE  FORMID='" + hfMainGuid.Value.Trim() + "'";
            //    DataAccess.Instance("BizDB").ExecuteDataTable(sqlDele);

            //}
            //try
            //{
            //    string strSql = "INSERT INTO PROC_CAPEX_PROJECT (FORMID,NTOTALINV,NLOCALEDT,NGLOBALEDT,NOPERATIONPROFIT,NROI,"
            //                            + "  CTOTALINV,CLOCALEDT,CGLOBALEDT,COPERATIONPROFIT,CROI,PROJECTCOMMENT)   "
            //                            + "  VALUES ('" + hfMainGuid.Value.Trim() + "','" + txt_NTOTALINV.Text.Trim() + "','" + txt_NLOCALEDT.Text.Trim() + "','" + txt_NGLOBALEDT.Text.Trim() + "',"
            //                            + "   '" + txt_NOPERATIONPROFIT.Text.Trim() + "','" + txt_NROI.Text.Trim() + "','" + txt_CTOTALINV.Text.Trim() + "','" + txt_CLOCALEDT.Text.Trim() + "',"
            //                            + "   '" + txt_CGLOBALEDT.Text.Trim() + "','" + txt_COPERATIONPROFIT.Text.Trim() + "','" + txt_CROT.Text.Trim() + "','" + txt_PROJECTCOMMENT.Text.Trim() + "')";
            //    DataAccess.Instance("BizDB").ExecuteDataTable(strSql);
            //}
            //catch (Exception ex)
            //{
            //    string strMess = ex.Message;
            //}            
        }


        /// <summary>
        /// 添加明细行数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            UserInfo userInfo = Page.FindControl("UserInfo1") as UserInfo;
            userInfo.AddNewRow(fld_detail_PROC_CAPEX_DT);
        }

        /// <summary>
        /// 删除明细行数据
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void fld_detail_Capex_DT_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            UserInfo userInfo = Page.FindControl("UserInfo1") as UserInfo;
            userInfo.DeleteRow(fld_detail_PROC_CAPEX_DT, e);
        }


        /// <summary>
        /// 绑定项目负责人
        /// </summary>
        public void BindddlProjectLeader()
        {
            DataTable dt = capex.Excel();
            fld_PROJECTLEADERAD.Items.Clear();

            fld_PROJECTLEADERAD.DataSource = dt;
            fld_PROJECTLEADERAD.DataTextField = "CNNAME";
            fld_PROJECTLEADERAD.DataValueField = "LOGINNAME";
            //fld_PROJECTLEADER.DataValueField = "CNNAME";
            fld_PROJECTLEADERAD.DataBind();
            fld_PROJECTLEADERAD.Items.Insert(0, new ListItem("--- 请选择 ---", ""));
        }

        /// <summary>
        /// 绑定项目支出原因
        /// </summary>
        void BindddlProjectReason()
        {
            DataTable dt = capex.GetProjectReason();
            fld_PROJECTREASON.DataSource = dt;
            fld_PROJECTREASON.DataTextField = "REASON";
            fld_PROJECTREASON.DataValueField = "REASON";
            fld_PROJECTREASON.DataBind();
            fld_PROJECTREASON.Items.Insert(0, new ListItem("--- 请选择 ---", ""));
        }
        
        /// <summary>
        /// 初始化成本中心
        /// </summary>
        /// <param name="UesrID"></param>
        /// <returns></returns>
        public string InitCostCenter(int UesrID, string strDept)
        {
            string strCostCenter = "";
            DataTable dt_detail = null;
            string strSql = "select EXT01 from COM_USER where USERID=" + UesrID + "";
            dt_detail = DataAccess.Instance("BizDB").ExecuteDataTable(strSql);
            if (dt_detail.Rows.Count > 0)
            {
                strCostCenter = dt_detail.Rows[0]["EXT01"].ToString().Trim();
            }             
            return strCostCenter;
        }

        /// <summary>
        /// 获取当前成本中心是否属于运营部门，1代表是，0代表否
        /// </summary>
        /// <param name="strCostCenter"></param>
        /// <returns></returns>
        public string JudgeIsOpreation(string strCostCenter)
        {
            string strResult = "";
            DataTable dt = null;
            string strSql = "SELECT COSTCENTERNUMBER, COSTCENTERNAME, DEPTID, DEPTNAME, ISOPERATION FROM SYNC_COSTCENTER WHERE (COSTCENTERNUMBER = '" + strCostCenter.Trim() + "') AND (ISOPERATION = '1')";
            dt = DataAccess.Instance("BizDB").ExecuteDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                strResult = "1";
            }
            else
            {
                strResult = "0";
            }
            return strResult;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void fld_detail_PROC_CAPEX_DT_PreRender(object sender, EventArgs e)
        {
            foreach (RepeaterItem ri in this.fld_detail_PROC_CAPEX_DT.Items)
            {
                RepeaterItem currentItem = ri;
                #region 绑定项目负责人
                DropDownList ddlPROJECTLEADER = currentItem.FindControl("ddlXMFZR") as DropDownList;
                ddlPROJECTLEADER.AppendDataBoundItems = true;
                ddlPROJECTLEADER.Items.Clear();
                ddlPROJECTLEADER.Items.Insert(0, new ListItem("---请选择---", ""));              
                DataTable dt = capex.Excel();
                ddlPROJECTLEADER.DataSource = dt;
                ddlPROJECTLEADER.DataTextField = "CNNAME";
                ddlPROJECTLEADER.DataValueField = "LOGINNAME";
                //ddlPROJECTLEADER.DataValueField = "CNNAME";
                ddlPROJECTLEADER.DataBind();

                DropDownList ddlXMFZR = ((DropDownList)ri.FindControl("ddlXMFZR"));
                string selectText = ((TextBox)ri.FindControl("fld_PROJECTLEADERNAME")).Text;
                if (selectText != "")
                {
                    ddlXMFZR.Items.FindByValue(selectText).Selected = true;
                }

                //DropDownList ddlBudgetType = ((DropDownList)ri.FindControl("fld_BUDGETTYPE"));
                //string selectBudgetTypeText = ((TextBox)ri.FindControl("txtBUDGETTYPE")).Text;
                //if (selectBudgetTypeText != "")
                //{
                //    ddlBudgetType.Items.FindByValue(selectBudgetTypeText).Selected = true;
                //}
                #endregion
            }
        }       
    }
}